﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace TTBox
{
    public partial class InputPanel : UserControl
    {

        bool m_bLogined = false;
        public InputPanel()
        {
            InitializeComponent();
            nLoc = 0;
            button_Type1_Click(null, null);
        }

        string m_strLastData = "";
        string m_strTypeTitle = "簽單號碼;跳錶金額;議價金額";
        int[] m_nInputLen = { 10, 5, 5 };
        int m_nTypeIndex = 0;

        public void initial(int arg_nType, string arg_strData)
        {
            LogManager.getInstance().Log("IP init:" + arg_nType.ToString() + ":" + arg_strData);
            m_strLastData = arg_strData;
            this.panel_Confirm.Top = 15;
            this.panel_Confirm.Left = 22;
            this.panel_Confirm.Visible = false;

            nLoc = 0;
            button_Type1_Click(null, null);
            this.textBox_Data.Text = "";

            this.panel_Confirm.Visible = false;
            this.label_Title.Text = "請輸入：";

            string[] strary = m_strTypeTitle.Split(';');
            m_nTypeIndex = arg_nType;
            this.label_Title.Text += strary[m_nTypeIndex];


            if (m_nTypeIndex == 0)
            {
                this.button_Abort.Text = "退出";
                this.button_OK.Text = "確定";
            }
            else if (m_nTypeIndex < m_nInputLen.Length - 1)
            {
                this.button_Abort.Text = "退回";
                this.button_OK.Text = "確定";

            }
            else if (m_nTypeIndex == m_nInputLen.Length - 1)
            {
                this.button_Abort.Text = "退回";
                this.button_OK.Text = "送出";
            }


        }

        private void button_Input_Click(object sender, EventArgs e)
        {
            if (m_nInputLen[m_nTypeIndex] <= this.textBox_Data.Text.Length)
                return;
            string str = ((Button)sender).Text;
            this.textBox_Data.Text += str;
        }

        private void button_Login_Clear_Click(object sender, EventArgs e)
        {
            this.textBox_Data.Text = "";

        }
        string strFull = "０１２３４５６７８９ＱＷＥＲＴＹＵＩＯＰＡＳＤＦＧＨＪＫＬＺＸＣＶＢＮＭ＿";
        string strHalf =  "0123456789QWERTYUIOPASDFGHJKLZXCVBNM_" ;
        private void button_Confirm_Click(object sender, EventArgs e)
        {
            string strResult = "";
            for (int i = 0; i < this.textBox_Data.Text.Length; i++)
            {
                int n = strFull.IndexOf(this.textBox_Data.Text[i]);
                strResult += strHalf[n];
            }


            if (m_nTypeIndex < m_nInputLen.Length-1)
            {
                initial(m_nTypeIndex+1, m_strLastData + strResult + "!");

            }
            else if(m_nTypeIndex == m_nInputLen.Length-1)
            {
                LogManager.getInstance().Log("Bill:" + m_strLastData + strResult);
                string[] straryData = m_strLastData.Substring(15).Split('!');

                this.label_BillNo.Text = "簽單：" + straryData[0];
                this.label_TaxiFee.Text = "跳錶：" + straryData[1];
                this.label_ContractFee.Text = "議價：" + strResult;

                this.Tag = m_strLastData + strResult;
                ButtonControl(false);
                this.panel_Confirm.Visible = true;

            }

        }

        private string getFullText(string arg_strHalf)
        {
            LogManager.getInstance().Log("GFT:" + arg_strHalf);
            string strResult = "";
            for (int i = 0; i < arg_strHalf.Length; i++)
            {
                int n = strHalf.IndexOf(arg_strHalf[i]);
                strResult += strFull[n];
            }
            return strResult;

        }

        private void button_Cancel_Click(object sender, EventArgs e)
        {
            this.Tag = "";
            this.panel_Confirm.Visible = false;
        }


        private void button_Abort_Click(object sender, EventArgs e)
        {
            LogManager.getInstance().Log("Abort:" + m_nTypeIndex.ToString());
            if (m_nTypeIndex == 0)
            {
                this.Tag = "";
                Form_Main.Main.ChangeToDispatchMsg();
            }
            else if (m_nTypeIndex == 1)
            {
                string strTemp = m_strLastData.Substring(15).Split('!')[0];
                initial(m_nTypeIndex - 1, m_strLastData.Substring(0, 15));
                this.textBox_Data.Text = getFullText(strTemp);
            }
            else if (m_nTypeIndex < m_nInputLen.Length)
            {
                string strTemp = m_strLastData.Split('!')[1];
                initial(m_nTypeIndex - 1, m_strLastData.Substring(0, m_strLastData.LastIndexOf('!')));
                this.textBox_Data.Text = getFullText(strTemp);
            }


        }

        private void button_Type2_Click(object sender, EventArgs e)
        {
            nLoc = 0;
            button_Type1_Click(sender, e);
        }

        static public string ButtonText = "１２３４５６７８９０ＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺ";

        int nLoc = 0;
        private void button_Type1_Click(object sender, EventArgs e)
        {
            this.button_Login_1.Text = getButtonText();
            this.button_Login_2.Text = getButtonText();
            this.button_Login_3.Text = getButtonText();
            this.button_Login_4.Text = getButtonText();
            this.button_Login_5.Text = getButtonText();
            this.button_Login_6.Text = getButtonText();
            this.button_Login_7.Text = getButtonText();
            this.button_Login_8.Text = getButtonText();
            this.button_Login_9.Text = getButtonText();
            this.button_Login_0.Text = getButtonText();
            this.button_Login_A.Text = getButtonText();
            this.button_Login_B.Text = getButtonText();

            if (nLoc > ButtonText.Length) nLoc = 0;
        }

        private string getButtonText()
        {
            if (nLoc > ButtonText.Length-1) nLoc = 0;
            return ButtonText.Substring(nLoc++, 1);

        }

        private void button_BS_Click(object sender, EventArgs e)
        {
            if(this.textBox_Data.Text.Length > 0)
                this.textBox_Data.Text = this.textBox_Data.Text.Remove(this.textBox_Data.Text.Length-1,1);
        }

        private void button_Clear_Click(object sender, EventArgs e)
        {
            this.textBox_Data.Text = "";
        }

        private void button_ConfirmSend_Click(object sender, EventArgs e)
        {
            this.panel_Confirm.Visible = false;

            string strResult = CmdTalker.getInstance().SendServerAndWaitResponse(Form_Main.Main.getCurrStatusChar() +"94" + this.Tag.ToString() , 10, "MOK", true);

            if (strResult != "MOK")
            {
                LogManager.getInstance().Log("傳送失敗:" + this.Tag.ToString());
            }
            else
            {
                ButtonControl(true);
                Form_Main.Main.ChangeToMainScreen();
            }
        }
        private void ButtonControl(bool arg_bEnabled)
        {
            foreach (Control c in this.Controls)
            {
                if (c is Button)
                {
                    c.Enabled = arg_bEnabled;
                }
            }

        }
        private void button_ConfirmCancel_Click(object sender, EventArgs e)
        {
            this.Tag = "";
            this.panel_Confirm.Visible = false;
            ButtonControl(true);

        }

        private void textBox_Data_GotFocus(object sender, EventArgs e)
        {
            if(this.button_Abort.Enabled)
                this.button_Abort.Focus();
            else
                this.button_ConfirmCancel.Focus();

        }


    }
}
